import data from './data'
const app = getApp()
const $ = app.globalData
Page({
	data: {
		options1: data,
		value1: [],
		userinfo:[],
		name:"",
		phone:"",
		add:[],
		xiangxi:"",
		tips_warn: false,
		tips_primary: false
	},
	onOpen1() {
		this.setData({ visible1: true })
	},
	onClose1() {
		this.setData({ visible1: false })
	},
	// 地址
	onChange1(e) {
		this.setData({ title1: e.detail.options.map((n) => n.label).join('/'),add:e.detail.options})
		console.log('onChange1', e.detail)
	},
	// 收货人姓名
	nameinput:function(e){
		this.setData({
			name:e.detail.value
		})
	},
	// 收件人手机号
	phoneinput:function(e){
		this.setData({
			phone:e.detail.value
		})
	},
	// 详细地址
	addinput:function(e){
		this.setData({
			xiangxi:e.detail.value
		})
	},
	// 保存
	newadd:function(){
		var that= this
		if(that.data.name == ""){
			that.setData({tips_warn:true,error:"请输入收货人姓名"})
			setTimeout(function(){that.setData({tips_warn:false})}, 3000);
		}else if(that.data.phone == "" || that.data.phone[0]!='1' || that.data.phone.length!=11){
			that.setData({tips_warn:true,error:"请输入正确的手机号"})
			setTimeout(function(){that.setData({tips_warn:false})}, 3000);
		}else if(that.data.title1 == ""){
			that.setData({tips_warn:true,error:"请选择地址"})
			setTimeout(function(){that.setData({tips_warn:false})}, 3000);
		}else if(that.data.xiangxi == ""){
			that.setData({tips_warn:true,error:"请输入详细地址"})
			setTimeout(function(){that.setData({tips_warn:false})}, 3000);
		}else{
			console.log(that.data.userinfo)
			var thataddid = that.data.thataddid
			if(thataddid){
				var json = {customerid:that.data.userinfo.id,name:that.data.name,phone:that.data.phone,province:that.data.add[0].label,city:that.data.add[1].label,district:that.data.add[2].label,address:that.data.xiangxi,state:that.data.start,id:thataddid}
			}else{
				var json = {customerid:that.data.userinfo.id,name:that.data.name,phone:that.data.phone,province:that.data.add[0].label,city:that.data.add[1].label,district:that.data.add[2].label,address:that.data.xiangxi,state:0}
			}
			wx.request({
				url:$.AdminUrl+"/YApi/addMyAddress",
				data:json,
				success:function(data){
					console.log(data)
					if(data.data.code){
						that.setData({tips_primary:true})
						setTimeout(function(){
							that.setData({tips_primary:false}),
							wx.navigateBack({})
						}, 3000);
					}
				}
			})
		}
	},
	onLoad: function(options) {
		var that = this
		// 判断是否已经登陆
		wx.getStorage({
			key:"userinfo",
			success:function(data){
				console.log(data)
				that.setData({
					userinfo:data.data
				})
			}
		})
		// 判断是否是编辑
		var thisid = options.id
		if(thisid){
			that.setData({
				thataddid:thisid
			})
			wx.request({
				url:$.AdminUrl+"/YApi/getOneAddress",
				data:{
					addressid:thisid
				},success:function(data){
					var arr = [{label:""},{label:""},{label:""}]
					arr[0].label = data.data.obj.province
					arr[1].label = data.data.obj.city
					arr[2].label = data.data.obj.district
					that.setData({
						start:data.data.obj.state,
						name:data.data.obj.name,
						phone:data.data.obj.phone,
						xiangxi:data.data.obj.address,
						add:arr,
						title1:arr[0].label+"/"+arr[1].label+"/"+arr[2].label
					})
					console.log(data)
				}
			})
		}else{
			
		}
	},
	// 分享转发
		onShareAppMessage: function() {
			var userinfo = wx.getStorageSync('userinfo')
			return {
				title: '优悦购生活管家',
				path: '/pages/index/index?userid='+userinfo.loginname,
				success: (res) => {
					console.log("转发成功", res);
				},
				fail: (res) => {
					console.log("转发失败", res);
				}
			}
		}
})